package ccmis.finance.capital.dao;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.QueryHint;

import jfox.platform.infrastructure.EntityObject;
import jfox.platform.infrastructure.SuperDAO;

import org.jfox.entity.QueryExt;

import ccmis.finance.capital.entity.InterestRate;


/**
 * @author <a href="mailto:ren.yingb@gmail.com">Ren Yingbo</a>
 */
@Stateless
@Local
@NamedNativeQueries(
		{
			@NamedNativeQuery(
					name = InterestRateDAOBean.GET_INTERESTRATE_BY_ID,
					query = "SELECT * FROM T_FINANCIAL_RATE_CHANGE WHERE ID=$ID AND VSTATUS=" + EntityObject.VSTATUS_NORMAL,
					resultClass = InterestRate.class,
					hints = {
					@QueryHint(name = "cache.partition", value = "function")
					        }
			),
			@NamedNativeQuery(
					name = InterestRateDAOBean.GET_ALL_INTERESTRATES,
					query = "SELECT * FROM T_FINANCIAL_RATE_CHANGE WHERE VSTATUS=" + EntityObject.VSTATUS_NORMAL + " ORDER BY ID",
					resultClass = InterestRate.class,
					hints = {
					@QueryHint(name = "cache.partition", value = "function")
					        }
			),
			@NamedNativeQuery(
					name = InterestRateDAOBean.INSERT_INTERESTRATE,
					query = "INSERT INTO T_FINANCIAL_RATE_CHANGE(" +
					        "ID," +
                            "PRIORITY, " +
                            "VSTATUS, " +
                            "CONTRACT_ID," +
                            "RNO," +
                            "START_DATE," +
                            "END_DATE," +
                            "INTEREST_RATE)" +
                            "VALUES(" +
                            "$INTERESTRATE.getId()," +
                            "$INTERESTRATE.getPriority()," +
                            "$INTERESTRATE.getVstatus()," +
                            "$INTERESTRATE.getContractID()," +
                            "$INTERESTRATE.getRNo()," +
                            "$INTERESTRATE.getStartDate()," +
                            "$INTERESTRATE.getEndDate()," +
                            "$INTERESTRATE.getInterestRate() )",
                    hints = {
                    @QueryHint(name = "cache.partition", value = "function")
                            }
			),
			@NamedNativeQuery(
					name = InterestRateDAOBean.DELETE_INTERESTRATE,
					query = "UPDATE T_FINANCIAL_RATE_CHANGE SET VSTATUS = " + EntityObject.VSTATUS_REMOVED + " WHERE ID=$ID",
					resultClass = InterestRate.class,
					hints = {
					@QueryHint(name = "cache.partition",value = "function")
					        }
			),
			@NamedNativeQuery(
					name = InterestRateDAOBean.UPDATE_INTERESTRATE,
					query = "UPDATE T_FINANCIAL_RATE_CHANGE" +
                            " SET PRIORITY = " + "$INTERESTRATE.getPriority()," +
                            " VSTATUS = " + "$INTERESTRATE.getVstatus()," +
                            " CONTRACT_ID = " + "$INTERESTRATE.getContractID()," +
                            " RNO = " + "$INTERESTRATE.getRNo()," +
                            " START_DATE = " + "$INTERESTRATE.getStartDate()," +
                            " END_DATE = " + "$INTERESTRATE.getEndDate()," +
                            " INTEREST_RATE = " + "$INTERESTRATE.getInterestRate()" +
                            " WHERE ID=" + "$INTERESTRATE.getId()",
                    hints = {
                	@QueryHint(name = "cache.partition",value = "function")
                			}
			)
		}
		
)
public class InterestRateDAOBean extends SuperDAO implements InterestRateDAO{
	
	public final static String GET_INTERESTRATE_BY_ID = "getInterestRateById";
	public final static String GET_ALL_INTERESTRATES = "getAllInterestRate";
	public final static String INSERT_INTERESTRATE = "insertInterestRate";
	public final static String DELETE_INTERESTRATE = "deleteInterestRate";
	public final static String UPDATE_INTERESTRATE = "updateInterestRate";

	public int deleteInterestRate(long id) {
		Map<String,Object> params = new HashMap<String,Object>(1);
		params.put("ID", id);
		return executeNamedNativeUpdate(DELETE_INTERESTRATE,params);
	}

	public List<InterestRate> getAllInterestRates() {
		return (List<InterestRate>)processNamedNativeQuery(GET_ALL_INTERESTRATES, Collections.EMPTY_MAP);
	}

	public InterestRate getInterestRateById(long id) {
		return (InterestRate)getEntityObjectByColumn(GET_INTERESTRATE_BY_ID,"ID",id);
	}

	public void insertInterestRate(InterestRate interestRate) {
		Map<String,Object> params = new HashMap<String,Object>(1);
		params.put("INTERESTRATE", interestRate);
		executeNamedNativeUpdate(INSERT_INTERESTRATE,params);	
		
	}

	public int updateInterestRate(InterestRate interestRate) {
		Map<String,Object> params = new HashMap<String,Object>(1);
		params.put("INTERESTRATE", interestRate);
		return executeNamedNativeUpdate(UPDATE_INTERESTRATE,params);
	}

}
